﻿using _01_Tower4._0.CharacterBase;
using _01_Tower4._0.GameFrame.Manager;
using _01_Tower4._0.Interface;
using _01_Tower4._0.Items.Base;
using _01_Tower4._0.Items;
using _01_Tower4._0.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using _01_Tower4._0.Skills.Base;
using _01_Tower4._0.Skills;
using System.Xml.Linq;

namespace _01_Tower4._0.GameFrame.System
{
    internal class SkillSystem
    {
        public List<Skill> skills;
        int x, y, width, height;
        int selection, maxShow;
        int startIndex, endIndex;
        public static bool IsOpen;
        public static bool IsUse;
        IAttack target;
        Character caster;
        int clearIndex;

        public SkillSystem(int x, int y, int width, int height, Character caster)
        {
            skills = new List<Skill>();
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            this.caster = caster;
            selection = 0;
            maxShow = height - 2;
            startIndex = 0;
            endIndex = 0;

            EventSystem.Instance.AddEvent("奥术飞弹", () =>
            {
                Add(new ArcaneMissiles());
            });
            EventSystem.Instance.AddEvent("治疗魔法", () =>
            {
                Add(new Healing());
            });

        }
        public void Target(IAttack target)
        {
            this.target = target;
        }

        public void Init()
        {
            IsOpen = false;
            IsUse = false;
        }

        public void Add(Skill skill)
        {
            skills.Add(skill);
            MoveShowIndex(true);
        }

        public void Remove(Skill skill)
        {
            skills.Remove(skill);
            MoveShowIndex(false);
        }

        public void Input()
        {
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.W))
            {
                UpRoll();
                Clear();
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.S))
            {
                DownRoll();
                Clear();
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.Enter))
            {
                if(SceneManager.lastSceneName == "SceneMap1")
                {
                    InfoSystem.Instance.AddInfo("未找到有效目标",ConsoleColor.Red);
                    return;
                }
                if (startIndex + selection < skills.Count)
                {
                    Use(skills[startIndex + selection], target);
                    IsUse = true;
                }
                if (SceneManager.lastSceneName == "SceneCombat")
                {
                    IsOpen = false;
                    IsUse = true;
                    SceneManager.sceneName = "SceneCombat";
                }
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.Escape))
            {
                if (SceneManager.lastSceneName == "SceneCombat")
                {
                    IsOpen = false;
                    CombatSystem.SkillIsOpen = true;
                    SceneManager.sceneName = "SceneCombat";
                }
            }
        }


        public void Render()
        {
            clearIndex = 0;
            if (startIndex + selection < skills.Count)
            {
                Description(skills[startIndex + selection]);
            }
            Utility.DrawFrame(x, y, width, height, ConsoleColor.White);

            if (skills.Count > 0)
            {
                for (int i = startIndex, j = 0; i <= endIndex; i++, j++)
                {
                    Utility.MyPrint($"{skills[i].Name} Cd:{skills[i].Cd}", x + 2, y + 1 + j);
                }
            }
            Utility.MyPrint("技能", x + (width/2) - 2, y - 1);
            Utility.MyPrint(">", x + 1, y + 1 + selection);
            Utility.MyPrint("<", x + width - 2, y + 1 + selection);
        }

        public void Clear()
        {
            for(int j = 0; j < height + 1; j++)
            {
                for (int i = 0; i < width+16; i++)
                {
                    Utility.MyPrint(" ", x + i, y - 1 + j);
                }
            }
            Utility.MyPrint("                                      ", x + 2, y + height + 1);
            Utility.MyPrint("                                      ", x + 2, y + height + 2);
        }

        public void Update()
        {
            if (!IsOpen && clearIndex < 1)
            {
                Clear();
                clearIndex++;
            }
        }
        //public static int cd = 0;
        public void Use(Skill skill, IAttack target)
        {
            Type? type = skill.GetType().BaseType;
            if (type == typeof(AtkSkill))
            {
                ((AtkSkill)skill).Use(target, caster);

            }
            if (type == typeof(DefSkill))
            {
                ((DefSkill)skill).Use(caster);
            }
        }

        public void Description(Skill skill)
        {
            Utility.MyPrint($"<{skill.Name}>", x + 2, y + height + 1);
            Utility.MyPrint(skill.Description, x + 2, y + height + 2);
        }

        void UpRoll()
        {
            selection--;
            if (selection < 0)
            {
                selection = 0;
                // 将道具列表往下移一行
                if (skills.Count > maxShow)
                {
                    MoveShowIndex(true);
                }
            }
        }

        void DownRoll()
        {
            if (selection < skills.Count - 1)
                selection++;

            if (selection > maxShow - 1)
            {
                selection = maxShow - 1;
                // 将道具列表往上移一行
                if (skills.Count > maxShow)
                {
                    MoveShowIndex(false);
                }
            }
        }

        void MoveShowIndex(bool up)
        {
            int offset = 0;
            if (skills.Count > maxShow)
            {
                offset = maxShow - 1;
            }
            else
            {
                offset = skills.Count - 1;
            }

            if (up)
            {
                startIndex--;
                if (startIndex < 0)
                {
                    startIndex = 0;
                }
                endIndex = startIndex + offset;
            }
            else
            {
                endIndex++;
                if (endIndex > skills.Count - 1)
                {
                    endIndex = skills.Count - 1;
                }
                startIndex = endIndex - offset;
            }
        }
    }
}
